<?php 
$reqPath='';
//require_once("include/authen.php");
require_once($reqPath."class/m_report.php");
require_once($reqPath.'fpdf/fpdf.php');

$datefrom = $_POST['dFrom'];
$dateto = $_POST['dTo'];


class PDF extends FPDF
{
	var $datefrom;
	var $dateto;
	var $branch;
	var $type;
	
	function setHeaderCriteria($dtfrom , $dtto,$branch,$type){
		$this->datefrom=$dtfrom;
		$this->dateto=$dtto;
		$this->branch=$branch;
		$this->type=$type;
	}
	function Header()
	{
		//Arial bold 15
		$this->SetFont('tahoma','B',12);
		//Title
		if ($this->type=='0')
			$this->Cell(0,10,iconv( 'UTF-8','cp874' , 'รายงานรายได้รวม'),0,0,'C');
		else if ($this->type=='1')
			$this->Cell(0,10,iconv( 'UTF-8','cp874' , 'รายงานสินค้าขายดี'),0,0,'C');		
		else if ($this->type=='2')
			$this->Cell(0,10,iconv( 'UTF-8','cp874' , 'รายงาน Treatment ขายดี'),0,0,'C');
		else if ($this->type=='3')
			$this->Cell(0,10,iconv( 'UTF-8','cp874' , 'รายงาน  Set ขายดี'),0,0,'C');		
		else if ($this->type=='4')
			$this->Cell(0,10,iconv( 'UTF-8','cp874' , 'รายงาน Course ขายดี'),0,0,'C');
			
		$this->Ln(5);
		if( $this->type!="0"){
			$this->SetFont('tahoma','',11);
			$this->Cell(0,10,iconv( 'UTF-8','cp874' , 'ประจำสาขา  '. $this->branch),0,0,'C');
			$this->Ln(5);
		}
		$this->SetFont('tahoma','',11);
		$this->Cell(0,10,iconv( 'UTF-8','cp874' , 'ข้อมูลตั้งแต่วันที่ '. $this->datefrom .' ถึง '. $this->dateto),0,0,'C');
		//Line break
		$this->Ln(15);
	}

	//Page footer
	function Footer()
	{
		//Position at 1.5 cm from bottom
		$this->SetY(-15);
		//Arial italic 8
		$this->SetFont('Arial','I',8);
		//Page number
		$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'R');
	}

	//Colored table
	function FancyTable($header,$data)
	{
		//Colors, line width and bold font
		$this->SetFillColor(20,114,218);
		$this->SetTextColor(255);
		$this->SetDrawColor(114,175,242);
		$this->SetLineWidth(.1);
		$this->SetFont('','B');
		//Header
		//Array('no' , branch, amount , %amount);
		if ($this->type==0){
			$w=array(15,95,55,30);

			for($i=0;$i<count($header);$i++)
				$this->Cell($w[$i],7,iconv( 'UTF-8','cp874' , $header[$i]),1,0,'C',true);
		}else{
			$w=array(15,75,20,25,30,30);

			for($i=0;$i<count($header);$i++)
					$this->Cell($w[$i],7,iconv( 'UTF-8','cp874' , $header[$i]),1,0,'C',true);
		}
		$this->Ln();
		
		//Color and font restoration
		$this->SetFillColor(224,235,255);
		$this->SetTextColor(0);
		$this->SetFont('');
		//Data
		$fill=false;
		$i=1;
		$totalAmt=0;
		$totalPercent=0;
		$totalNoSale=0;
		if ($this->type==0){
		foreach($data as $row)
			{
			//	echo $row[0];
				$this->Cell($w[0],6,$i,'LR',0,'R',$fill);
				$this->Cell($w[1],6,iconv( 'UTF-8','cp874' , $row['Name']),'LR',0,'L',$fill);
				$this->Cell($w[2],6, number_format( $row['Amount'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[3],6, number_format( $row['Percent'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Ln();
				$fill=!$fill;
				$i++;
				$totalAmt+=$row['Amount'];
				$totalPercent+=$row['Percent'];
			}
			$this->Cell(array_sum($w),0,'','T');
			$this->Ln();
			$this->SetFont('','B');
		
			$this->Cell($w[0],6,'Total','LR',0,'R',$fill);
			$this->Cell($w[1],6,'','LR',0,'R',$fill);
			$this->Cell($w[2],6,number_format($totalAmt,2,'.',','),'LR',0,'R',$fill);
			$this->Cell($w[3],6,number_format($totalPercent ,0,'.',','),'LR',0,'R',$fill);
			$this->Ln();
			$this->Cell(array_sum($w),0,'','T');
		}else {
			foreach($data as $row)
			{
			//	echo $row[0];
				$this->Cell($w[0],6,$i,'LR',0,'R',$fill);
				$this->Cell($w[1],6,iconv( 'UTF-8','cp874' , $row['Name']),'LR',0,'L',$fill);
				$this->Cell($w[2],6, number_format( $row['NoSale'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[3],6, number_format( $row['Price'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[4],6, number_format( $row['Amount'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[5],6, number_format( $row['Percent'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Ln();
				$fill=!$fill;
				$i++;
				$totalNoSale+=$row['NoSale'];
				$totalAmt+=$row['Amount'];
				$totalPercent+=$row['Percent'];
			}
			$this->Cell(array_sum($w),0,'','T');
			$this->Ln();
			$this->SetFont('','B');
		
			$this->Cell($w[0]+$w[1],6,'Total','LR',0,'R',$fill);
			$this->Cell($w[2],6,number_format($totalNoSale,2,'.',','),'LR',0,'R',$fill);
			$this->Cell($w[3],6,'','LR',0,'R',$fill);
			$this->Cell($w[4],6,number_format($totalAmt,2,'.',','),'LR',0,'R',$fill);
			$this->Cell($w[5],6,number_format($totalPercent ,0,'.',','),'LR',0,'R',$fill);
			$this->Ln();
			$this->Cell(array_sum($w),0,'','T');
		}
	
		
	
		
	}
}
if (!(isset($_POST['dFrom']))) {
	header("location:report_topsale.php");
}

list($day, $month, $year) = split('/', $datefrom);
$c_from =$year."/".$month."/".$day;

list($day, $month, $year) = split('/', $dateto);
$c_to =$year."/".$month."/".$day;

$branch = $_POST['branch'];
$type=$_POST['type'];
 
 // $c_from='2011/01/01';
 // $c_to='2011/03/31';
 // $branch=0;
 // $type=1;
$report=new M_report();

$header=$report->TopSaleHeader($type);
$data=$report->TopSaleReport($c_from ,$c_to,$branch,$type);
if (count($data)==0){
	echo "<script>alert('Data not found');window.close();</script>";
	exit();
}
$branchName=$_POST['branchName']=="All"?"ทั้งหมด":$_POST['branchName'];
$pdf=new PDF();
$pdf->setHeaderCriteria($datefrom,$dateto,$branchName,$type);
$pdf->SetRightMargin(5);
$pdf->SetLeftMargin(5);
//Set pageno
$pdf->AliasNbPages();
$pdf->AddFont("tahoma","","tahoma.php");
$pdf->AddFont("tahoma","B","tahomab.php");

$pdf->SetFont("tahoma","B",10); 
$pdf->SetFont("tahoma","",10); 

//Data loading
$pdf->AddPage();
$pdf->FancyTable($header,$data );
$pdf->Output();
exit();
?>
